@include "globals.awk"
@include "readcsv.awk"
@include "dist.awk"
@include "lib.awk"
@include "tile.awk"

BEGIN { pairs("0.1,-,0.3, ,0.5, ,0.7,-,0.9, ",Chops)
        main() }


function main(w,gp,np,hi,lo,rows,all,d,x,xs) {
    readcsv(w,gp,np,hi,lo,rows,all,d) ; m=11
    k=0.5
    while(k < 8) {
	k = int(k*2)
	x = think(k,w,gp,np,hi,lo,rows,all,d,x,xs)
    }
    for(k in xs)
	print sprintf("%2s",k),Normalize, \
	      tile(xs[k],Chops,30,"%4s",0,450) | "sort -t, -n -k 8"

}
function think(k,w,gp,np,hi,lo,rows,all,d,max,xs,	\
               i,x) {
    for(i in all) {
        x= think1(k,all[i],w,gp,np,hi,lo,rows,all,d) 
	xs[k][i] = x
	if (x > max) max = x
    }
    return max
}
function think1(k,row1,w,gp,np,hi,lo,rows,all,d,  \
		n,near,i,m,klass,got,want) {
    klass= klass1(gp)
    n = kth(k,row1,w,gp,np,hi,lo,d,all, near)
    for(i in near)
	m += d[near[i]][klass]
    got  =  m/n
    want = d[row1][klass]
    return int(100*abs(want - got)/want)
    
}
